/* 
 * File:   utils.h
 * Author: manu
 *
 * Created on 24 avril 2010, 17:29
 */

#ifndef _UTILS_H
#define	_UTILS_H



#endif	/* _UTILS_H */

#include <stdlib.h>
//from package libopenal-dev
#include <AL/al.h>
#include <AL/alc.h>
#include <AL/alext.h>
//from package libalut-dev
#include <AL/alut.h>


ALuint Buffer;

    ALuint Source;
    // Position of the source sound.
    ALfloat SourcePos[] = { 0.0, 0.0, 0.0 };

    // Velocity of the source sound.
    ALfloat SourceVel[] = { 0.0, 0.0, 0.0 };


    // Position of the listener.
    ALfloat ListenerPos[] = { 0.0, 0.0, 0.0 };

    // Velocity of the listener.
    ALfloat ListenerVel[] = { 0.0, 0.0, 0.0 };

    // Orientation of the listener. (first 3 elements are "at", second 3 are "up")
    ALfloat ListenerOri[] = { 0.0, 0.0, -1.0,  0.0, 1.0, 0.0 };



 ALboolean LoadALData()
{
     

    ALenum format;
    ALsizei size;
    ALvoid* data;
    ALsizei freq;
    ALboolean loop;

    //alGenBuffers(1, &Buffer);
    if (alGetError() != AL_NO_ERROR)
        return AL_FALSE;

    //alutLoadWAVFile("data/crapaud.wav", &format, &data, &size, &freq, &loop);
    //alutLoadWAVFile("data/chimp.wav", &format2, &data2, &size2, &freq2, &loop2);

    //Buffer = alutCreateBufferHelloWorld();
    Buffer = alutCreateBufferFromFile("data/crapaud.wav");


    //std::cout << Buffer ;

    //std::cout << format <<'-' << size <<'-' << freq ;
    //alBufferData(Buffer, format, data, size, freq);
    //alutUnloadWAV(format, data, size, freq);
       // Bind buffer with a source.
    alGenSources(1, &Source);

    if (alGetError() != AL_NO_ERROR)
        return AL_FALSE;

    alBufferiv()

    alSourcei (Source, AL_BUFFER,   Buffer   );
    //alSourceQueueBuffers(Source,1,Buffer);

    

    alSourcef (Source, AL_PITCH,    1.0f     );
    alSourcef (Source, AL_GAIN,     1.0f     );
    alSourcefv(Source, AL_POSITION, SourcePos);
    alSourcefv(Source, AL_VELOCITY, SourceVel);

    

    alSourcei (Source, AL_LOOPING,  AL_FALSE     );

    if (alGetError() == AL_NO_ERROR)
        return AL_TRUE;


    return AL_FALSE;

 }

 ALboolean play()
 {
     //Buffer = alutCreateBufferHelloWorld() ;
     //Buffer = alutCreateBufferFromFile ("data/crapaud.wav");
     //alSourcei (Source, AL_BUFFER,   Buffer   );
     //alSourceQueueBuffers(Source,1,&Buffer);
 }


void SetListenerValues()
{
    alListenerfv(AL_POSITION,    ListenerPos);
    alListenerfv(AL_VELOCITY,    ListenerVel);
    alListenerfv(AL_ORIENTATION, ListenerOri);
}

void KillALData()
{
    alDeleteBuffers(1, &Buffer);
    alDeleteSources(1, &Source);
    alutExit();
}


/*
alGetSourcei(source, AL_SOURCE_STATE, &state);

    return (state == AL_PLAYING);
 * 
 *
 */

stream(ALuint buffer)
{
    char data[BUFFER_SIZE];
    int  size = 0;
    int  section;
    int  result;

    while(size < BUFFER_SIZE)
    {
        result = ov_read(&oggStream, data + size, BUFFER_SIZE - size, 0, 2, 1, & section);

        if(result > 0)
            size += result;
        else
            if(result < 0)
                throw oggString(result);
            else
                break;
    }

    if(size == 0)
        return false;

    alBufferData(buffer, format, data, size, vorbisInfo->rate);
    check();

    return false;
}